package backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 46. 全排列
 * User: Li_yizYa
 * Date: 2025/6/20
 * Time: 11:23
 */
public class Solution11 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> ret = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return ret;
        }
        List<Integer> tmp = new ArrayList<>();
        int[] used = new int[nums.length];
        backTracking(nums, ret, tmp, used);
        return ret;
    }
    private void backTracking(int[] nums, List<List<Integer>> ret, List<Integer> tmp, int[] used) {
        if (tmp.size() == nums.length) {
            ret.add(new ArrayList<>(tmp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i] == 1) {
                continue;
            }
            used[i] = 1;
            tmp.add(nums[i]);
            backTracking(nums, ret, tmp, used);
            used[i] = 0;
            tmp.remove(tmp.size() - 1);
        }
    }
}
